package com.cierzo.eagleband.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.xpath.*;

import org.xml.sax.InputSource;

import com.cierzo.eagleband.core.Servicio;

public class Configuracion {

	/**
	 * @param args
	 * @throws XPathExpressionException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, XPathExpressionException {
		// TODO Auto-generated method stub
		cargarConfiguracion("config.xml");
		System.out.println(Servicio.HOSTDB); 
	}
	public static boolean cargarConfiguracion(String path) throws FileNotFoundException {
		boolean config = false;
		XPathFactory  factory=XPathFactory.newInstance();
		XPath xPath=factory.newXPath();
		File xmlDocument = new File(path);	
		
		//convertir en funcion todo esto
		InputSource inputSourceHostDb = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourcePortDb = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceNombreDb = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceUsuarioDb = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourcePassDb = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceUsuario = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourcePass = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceOutputfile = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceMaxLoop = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceInterface = new InputSource(new  FileInputStream(xmlDocument));
		InputSource inputSourceFiltro = new InputSource(new  FileInputStream(xmlDocument));

		XPathExpression xPathHOSTDB;
		try {
			xPathHOSTDB = xPath.compile("/configuracion/database/host");
			XPathExpression  xPathPORTDB= xPath.compile("/configuracion/database/port");
			XPathExpression  xPathNOMBREDB= xPath.compile("/configuracion/database/nombre");
			XPathExpression  xPathUSUARIODB= xPath.compile("/configuracion/database/usuario");
			XPathExpression  xPathPASSDB= xPath.compile("/configuracion/database/pass");
			XPathExpression  xPathUSUARIO= xPath.compile("/configuracion/seguridad/usuario");
			XPathExpression  xPathPASS= xPath.compile("/configuracion/seguridad/pass");
			XPathExpression  xPathOUTPUTFILE= xPath.compile("/configuracion/outputfile");
			XPathExpression  xPathMAXLOOP= xPath.compile("/configuracion/pcap/maxloop");
			XPathExpression  xPathINTERFACE= xPath.compile("/configuracion/pcap/interface");
			XPathExpression  xPathFILTRO= xPath.compile("/configuracion/pcap/filtro");

			Servicio.HOSTDB = xPathHOSTDB.evaluate(inputSourceHostDb);
			Servicio.PORTDB = xPathPORTDB.evaluate(inputSourcePortDb);
			Servicio.NOMBREDB = xPathNOMBREDB.evaluate(inputSourceNombreDb);
			Servicio.USUARIODB = xPathUSUARIODB.evaluate(inputSourceUsuarioDb);
			Servicio.PASSDB = xPathPASSDB.evaluate(inputSourcePassDb);
			Servicio.USUARIO = xPathUSUARIO.evaluate(inputSourceUsuario);
			Servicio.PASS = xPathPASS.evaluate(inputSourcePass);
			Servicio.MAXLOOP = Integer.valueOf(xPathMAXLOOP.evaluate(inputSourceMaxLoop));
			Servicio.INTERFACE = Integer.valueOf(xPathINTERFACE.evaluate(inputSourceInterface));
			Servicio.FILTRO = xPathFILTRO.evaluate(inputSourceFiltro);
			
			Servicio.OUTPUTFILE = xPathOUTPUTFILE.evaluate(inputSourceOutputfile);
			config = true;
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}
		
		    
		return config;
	}
	
}
